﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Web;
//using System.IO;
//using Lucene.Net.Analysis.Standard;
//using ZealousTTS.Domain;
//using ZealousTTS.Web.ViewModels;
//using ZealousTTS.Common.Extensions;
//using Lucene.Net.Index;

//namespace ZealousTTS.Web.Utility
//{
//    /// <summary>
//    /// Lucene Search Engine Has been Removed fro the time being for lack of Documentation and Tutorials
//    /// Instead of SQL Server Full Text Search will be used to achieve this feature.
//    /// </summary>
//    public static class LuceneEngine
//    {
//        private static String _luceneIndexDirectory;
//        public static String LuceneIndexDirectory
//        {
//            get
//            {
//                return _luceneIndexDirectory;
//            }
//            set
//            {
//                String path = value;
//                if (!path.StartsWith("/"))
//                    path = String.Format("/{0}", path);
//                _luceneIndexDirectory = HttpContext.Current.Server.MapPath(path);
//            }
//        }

//        /// <summary>
//        /// Builds Lucene Search Index if thery were not generated yet.
//        /// </summary>
//        /// <param name="app"></param>
//        public static void BuildSearchIndex()//HttpApplicationState app)
//        {                        
//            System.Threading.Thread thread = new System.Threading.Thread(reBuildSearchIndex);
//            //thread.Start(app);
//            thread.Start();
//        }

//        public static StandardAnalyzer _Analyzer = new StandardAnalyzer();
//        private static IndexWriter _IndexWriter = null;

//        private static void reBuildSearchIndex()
//        {
//            _IndexWriter = new IndexWriter(LuceneIndexDirectory, _Analyzer, true);
//            AppEntities context = new AppEntities();
//            SavedState state = context.SavedStates.FirstOrDefault(P => String.Compare(P.StateName, ConfigReader.LuceneSavedStateName, true) == 0);
//            if (state == null || String.Compare(state.Value, "False", true) == 0)
//            {
//                List<ProductForReIndex> products = context.GetProductsForReIndex().ToList();

//                int count = products.Count();

//                if (!Directory.Exists(LuceneIndexDirectory))
//                    Directory.CreateDirectory(LuceneIndexDirectory);

//                foreach (ProductForReIndex product in products)
//                {
//                    _IndexWriter.AddDocument(createLuceneDoc(product));
//                }
//                _IndexWriter.Optimize();
//                _IndexWriter.Close();
//                ///Save the Search Index
//                if (state == null)
//                {
//                    state = new SavedState();
//                    state.StateName = ConfigReader.LuceneSavedStateName;
//                    context.AddToSavedStates(state);
//                }
//                state.Value = "True";
//                context.SaveChanges();
//            }
//        }

//        /// <summary>
//        /// Creates a Document from a Single Product Record
//        /// </summary>
//        /// <param name="product"></param>
//        /// <returns></returns>
//        private static Lucene.Net.Documents.Document createLuceneDoc(ProductForReIndex product)//int bug_id, string text)
//        {
//            Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
            
//            doc.Add(new Lucene.Net.Documents.Field(
//                "ID",
//                product.ID.ToString(),
//                Lucene.Net.Documents.Field.Store.YES,
//                Lucene.Net.Documents.Field.Index.NOT_ANALYZED)); //.UN_TOKENIZED));

//            doc.Add(new Lucene.Net.Documents.Field(
//                "Description",
//                new System.IO.StringReader(product.Description)));

//            doc.Add(new Lucene.Net.Documents.Field(
//                "Name",
//                new System.IO.StringReader(product.Name)));

//            // For the highlighter, store the raw text
//            doc.Add(new Lucene.Net.Documents.Field(
//                "Raw_Description",
//                product.Description,
//                Lucene.Net.Documents.Field.Store.YES,
//                Lucene.Net.Documents.Field.Index.NOT_ANALYZED));//.UN_TOKENIZED));

//            return doc;
//        }

//        public static void ModifyIndex(Product product, bool isDelete)
//        {            
//            _IndexWriter.DeleteDocuments(new Term("ID", product.ID.ToString()));
//            if (!isDelete) //This is a Update index Operation
//            {
//                ProductForReIndex p = new ProductForReIndex();
//                p.ID = product.ID;
//                p.Name = product.Name;
//                p.Description = product.Description;
//                Lucene.Net.Documents.Document doc = createLuceneDoc(p);
//                _IndexWriter.AddDocument(doc);
//            }
//            _IndexWriter.Commit();
//            _IndexWriter.Optimize();
//            _IndexWriter.Close();            
//        }
//        /// <summary>
//        /// Forces the Lucene Engine to Regenerate the Full Index
//        /// </summary>
//        public static void ForceReindex()
//        {
//            ///Delete Current Index
//            _IndexWriter.DeleteAll();
//            _IndexWriter.Commit();
//            ///Update the Index State in Database
//            AppEntities context = new AppEntities();
//            SavedState state = context.SavedStates.FirstOrDefault(P => String.Compare(P.StateName, ConfigReader.LuceneSavedStateName, true) == 0);
//            state.Value = "False";
//            context.SaveChanges();
            
//            ///Now Regenerate the Index
//            BuildSearchIndex();
//        }
//    }
//}